package com.yuba.front.controller.articles;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yuba.common.core.domain.R;
import com.yuba.common.core.validate.AddGroup;
import com.yuba.common.core.validate.EditGroup;
import com.yuba.common.excel.utils.ExcelUtil;
import com.yuba.common.idempotent.annotation.RepeatSubmit;
import com.yuba.common.log.annotation.Log;
import com.yuba.common.log.enums.BusinessType;
import com.yuba.common.mybatis.core.page.PageQuery;
import com.yuba.common.mybatis.core.page.TableDataInfo;
import com.yuba.common.web.core.BaseController;
import com.yuba.front.domain.bo.ArticlesBo;
import com.yuba.front.domain.vo.ArticlesVo;
import com.yuba.front.service.IArticlesService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 钓鱼知识库
 *
 * @author modakai
 * @date 2024-12-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/front/articles")
public class ArticlesController extends BaseController {

    private final IArticlesService articlesService;

    /**
     * 查询钓鱼知识库列表
     */
    @SaCheckPermission("front:articles:list")
    @GetMapping("/list")
    public TableDataInfo<ArticlesVo> list(ArticlesBo bo, PageQuery pageQuery) {
        return articlesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出钓鱼知识库列表
     */
    @SaCheckPermission("front:articles:export")
    @Log(title = "钓鱼知识库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ArticlesBo bo, HttpServletResponse response) {
        List<ArticlesVo> list = articlesService.queryList(bo);
        ExcelUtil.exportExcel(list, "钓鱼知识库", ArticlesVo.class, response);
    }

    /**
     * 获取钓鱼知识库详细信息
     *
     * @param articleId 主键
     */
    @SaCheckPermission("front:articles:query")
    @GetMapping("/{articleId}")
    public R<ArticlesVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long articleId) {
        return R.ok(articlesService.queryById(articleId));
    }

    /**
     * 新增钓鱼知识库
     */
    @SaCheckPermission("front:articles:add")
    @Log(title = "钓鱼知识库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ArticlesBo bo) {
        return toAjax(articlesService.insertByBo(bo));
    }

    /**
     * 修改钓鱼知识库
     */
    @SaCheckPermission("front:articles:edit")
    @Log(title = "钓鱼知识库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ArticlesBo bo) {
        return toAjax(articlesService.updateByBo(bo));
    }

    /**
     * 删除钓鱼知识库
     *
     * @param articleIds 主键串
     */
    @SaCheckPermission("front:articles:remove")
    @Log(title = "钓鱼知识库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{articleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] articleIds) {
        return toAjax(articlesService.deleteWithValidByIds(List.of(articleIds), true));
    }
}
